<template>
  <view class="good-list">
    <view class="good-item" v-for="(item,index) in goodList" :key="item.goods_id" @click="gotoGoodDetail(item)">
      <my-goods :item="item"></my-goods>
    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          //搜索关键词
          query: '',
          //分类id
          cid: 0,
          pagenum: 1,
          pagesize: 10
        },
        //节流阀
        isloading: true,
        goodList: [],
        total: 0
      };
    },
    methods: {
      onLoad(options) {
        this.queryObj.query = options.query || ''
        this.queryObj.cid = options.cid || ''
        this.getGoodsList()
      },

      async getGoodsList(cb) {
        //设置节流阀
        if (this.isloading) {
          this.isloading = false
          let {
            data: res
          } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
          if (res.meta.status === 200) {
            this.goodList = [...this.goodList, ...res.message.goods]
            this.total = res.message.total
          } else return uni.$showMsg()
          //如果cb存在就调用cb函数
          cb && cb()
          this.isloading = true
        }
      },

      //上拉触顶 —— 重新获取数据
      onReachBottom() {
        //正在请求相应的数据
        if (!this.isloading) return
        //说明没有新数据了
        if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
          uni.showToast({
            title: '没有更多的数据了...'
          })
          return
        }
        this.queryObj.pagenum += 1
        //再次发请求，获取新数据
        this.getGoodsList()
      },

      //下拉刷新 —— 获取更多的数据
      onPullDownRefresh() {
        this.goodList = []
        this.total = 0
        this.pageSize = 10

        //再次发请求 暂停下拉效果
        this.getGoodsList(() => uni.stopPullDownRefresh())

      },

      //点击商品跳转到商品详情页
      gotoGoodDetail(item) {
        uni.navigateTo({
          url: `/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`
        })
      },


    }
  }
</script>

<style lang="scss">

</style>
